package com.dna.controller;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dna.entity.MenuEntity;
import com.dna.entity.UserEntity;
import com.dna.service.MenuService;
import com.dna.utils.R;
import com.dna.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 登录相关
 * 
 */
@RestController
@RequestMapping("/menu")
@Api("菜单信息")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("user")
    @ApiOperation(value = "根据用户权限查询菜单树")
    public R user() {

        UserEntity userEntity = ShiroUtils.getUserEntity();

        List<MenuEntity> menuList = menuService.queryByUser(userEntity);

        return R.ok().put("data", menuList);
    }

    @GetMapping("list")
    @ApiOperation(value = "查询菜单列表")
    public R list() {

        List<MenuEntity> menuList = menuService.queryAll();

        return R.ok().put("data", menuList);
    }

    @PostMapping
    @ApiOperation(value = "保存菜单")
    public R saveMenu(MenuEntity me) {
        MenuEntity menuEntity = menuService.queryOne(me);
        if (menuEntity != null) {
            return R.error(me.getName() + " 已存在!");
        }
        menuService.save(me);
        return R.ok();

    }

    @PutMapping
    @ApiOperation(value = "更新菜单")
    public R updateMenu(MenuEntity me) {
        menuService.update(me);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除菜单")
    public R delete(@RequestBody Set<Long> ids) {
        System.out.println("ids====" + ids);
        menuService.delete(ids);
        return R.ok();
    }
}